<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="版本" prop="version">
        <el-select v-model="queryParams.version" clearable @change="handleQuery">
          <el-option
            v-for="dict in sys_topic_version"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:hairTriggerEvent:export']"-->
<!--        >导出-->
<!--        </el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="24" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postShareList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="问题类型" align="center" prop="topicType">
        <template #default="scope">
          <dict-tag :options="sys_topic_type" :value="scope.row.topicType"/>
        </template>
      </el-table-column>
      <el-table-column label="问题内容" align="center" prop="topicContent"/>
      <el-table-column label="总参与问卷数" align="center" prop="answerCount"/>
      <el-table-column label="回答完成数" align="center" prop="completeCount"/>
      <el-table-column label="回答率" align="center" prop="answerRate">
        <template #default="scope">
          <span>{{ scope.row.answerRate }}%</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="HairTriggerEvent">
import {shareAllBoard} from "@/api/business/hairTriggerEvent";
import {
  getAnswersStatistics,
} from "@/api/business/operation";
import {parseConvertTime} from "@/utils/ruoyi";

const {proxy} = getCurrentInstance();
const {sys_topic_type, sys_topic_version} = proxy.useDict("sys_topic_type", "sys_topic_version");

const postShareList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 15,
    version: "1",
    userId: undefined,
    type: undefined,
    startDate: undefined,
    endDate: undefined,
    nickName: undefined,
    salesDirector: undefined,
    eventValue: undefined,
    theirMilitary: undefined,
  },
  rules: {
    id: [{required: true, message: "主键id不能为空", trigger: "blur"}],
    userId: [{required: true, message: "用户id不能为空", trigger: "blur"}],
    type: [{required: true, message: "事件类型不能为空", trigger: "change"}],
    startDate: [
      {required: true, message: "触发时间不能为空", trigger: "blur"},
    ],
    eventValue: [
      {required: true, message: "事件产物,记录值不能为空", trigger: "blur"},
    ],
  },
});

const {queryParams, form, rules} = toRefs(data);

/** 查询用户触发事件记录列表 */
function getList() {
  loading.value = true;
  getAnswersStatistics(queryParams.value).then((response) => {
    console.log(response);
    for(var i = 0;i<response.rows.length;i++){
      let richTextRegex = /<(p)(.*?)>/g;
      if(richTextRegex.test(response.rows[i].topicContent)){
        response.rows[i].topicContent = extractTextFromRichContent(response.rows[i].topicContent);
      }
    }
    postShareList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function extractTextFromRichContent(richContent) {
  // 创建一个新的DOM解析器
  const parser = new DOMParser();
  // 解析富文本内容为文档对象
  const doc = parser.parseFromString(richContent, 'text/html');
  // 提取文本内容
  return doc.body.textContent || '';
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    type: null,
    startDate: null,
    endDate: null,
    nickName: null,
    salesDirector: null,
    eventValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
  };
  queryParams.value.version = '1'
  proxy.resetForm("hairTriggerEventRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");

  queryParams.value = {
    pageNum: 1,
    pageSize: 15,
    userId: null,
    type: null,
    startDate: null,
    endDate: null,
    nickName: null,
    salesDirector: null,
    eventValue: null,
    theirMilitary: null,
  };
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/business/operation/exportPostDapList",
    {
      ...queryParams.value,
    },
    `hairTriggerEvent_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
